package org.jeecg.modules.wlhy.hycustomeragent.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DictUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.hycustomeragent.entity.HyCustomerAgent;
import org.jeecg.modules.wlhy.hycustomeragent.service.IHyCustomerAgentService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.wlhy.hyproject.service.IHyProjectService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: hy_customer_agent
 * @Author: jeecg-boot
 * @Date:   2021-11-08
 * @Version: V1.0
 */
@Api(tags="hy_customer_agent")
@RestController
@RequestMapping("/hycustomeragent/hyCustomerAgent")
@Slf4j
public class HyCustomerAgentController extends JeecgController<HyCustomerAgent, IHyCustomerAgentService> {
	@Autowired
	private IHyCustomerAgentService hyCustomerAgentService;

	@Autowired
	private UserUtil userUtil;

	 @Autowired
	 private DictUtil dictUtil;
	 @Autowired
	 private IHyProjectService hyProjectService;

	@ApiOperation(value="hy_customer_agent-分页列表查询", notes="hy_customer_agent-分页列表查询")
	@GetMapping(value = "/list")
	@RequiresPermissions("agent:list")
	public Result<?> queryPageList(HyCustomerAgent hyCustomerAgent,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyCustomerAgent> queryWrapper = QueryGenerator.initQueryWrapper(hyCustomerAgent, req.getParameterMap());

		Page<HyCustomerAgent> page = new Page<HyCustomerAgent>(pageNo, pageSize);
		IPage<HyCustomerAgent> pageList = hyCustomerAgentService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	//根据货主id获取货主的经纪人列表，
	 @GetMapping(value = "/getAgentList")
	 public Result<?> getAgentList(@RequestParam(name="customerId", defaultValue="") String customerId) {
		 if(!userUtil.isSys()){
			 HyCustomer customer = userUtil.getCustomer();
			 if(customer == null){
			 	return Result.error("货主信息不存在");
			 }
			 customerId = customer.getId();
		 }
		 if(StringUtils.isEmpty(customerId)){
			 return Result.error("货主id不能为空");
		 }
		 List<HyCustomerAgent> hyCustomerAgentList = hyCustomerAgentService.getListByCustomerId(customerId);
		 List<?> list = dictUtil.parseArrayListDict(hyCustomerAgentList);
		 return Result.OK(list);
	 }

	 //根据经纪人id获取经纪人的货主列表，
	 @GetMapping(value = "/getAgentCustomerList")
	 public Result<?> getAgentCustomerList(@RequestParam(name="customerId", defaultValue="") String customerId) {
		 if(userUtil.isSys()){
			 List<HyCustomerAgent> hyCustomerAgentList = hyCustomerAgentService.list();
			 List<?> list = dictUtil.parseArrayListDict(hyCustomerAgentList);
			 return Result.OK(list);
		 }
		 HyCustomer customer = userUtil.getCustomer();
		 if(customer == null){
			 return Result.error("货主信息不存在");
		 }
		 customerId = customer.getId();
		 if(StringUtils.isEmpty(customerId)){
			 return Result.error("货主id不能为空");
		 }
		 List<HyCustomerAgent> hyCustomerAgentList = hyCustomerAgentService.getListByAgentId(customerId);
		 List<?> list = dictUtil.parseArrayListDict(hyCustomerAgentList);
		 return Result.OK(list);
	 }



	 /**
	 *   添加
	 *
	 * @param hyCustomerAgent
	 * @return
	 */
	@AutoLog(value = "hy_customer_agent-添加")
	@ApiOperation(value="hy_customer_agent-添加", notes="hy_customer_agent-添加")
	@PostMapping(value = "/add")
	@RequiresPermissions("agent:list")
	public Result<?> add(@RequestBody HyCustomerAgent hyCustomerAgent) {
		HyCustomerAgent customerAgent = hyCustomerAgentService.getOne(new QueryWrapper<HyCustomerAgent>()
				.eq("customer_id",hyCustomerAgent.getCustomerId())
				.eq("agent_id",hyCustomerAgent.getAgentId()));
		if(customerAgent!=null){
			return Result.error("您已添加过该经纪人！");
		}else{
			hyCustomerAgentService.save(hyCustomerAgent);
			return Result.OK("添加成功！");
		}
	}

	/**
	 *  编辑
	 *
	 * @param hyCustomerAgent
	 * @return
	 */
	@AutoLog(value = "hy_customer_agent-编辑")
	@ApiOperation(value="hy_customer_agent-编辑", notes="hy_customer_agent-编辑")
	@PutMapping(value = "/edit")
	@RequiresPermissions("agent:list")
	public Result<?> edit(@RequestBody HyCustomerAgent hyCustomerAgent) {
		hyCustomerAgentService.updateById(hyCustomerAgent);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "hy_customer_agent-通过id删除")
	@ApiOperation(value="hy_customer_agent-通过id删除", notes="hy_customer_agent-通过id删除")
	@DeleteMapping(value = "/delete")
	@RequiresPermissions("agent:list")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hyCustomerAgentService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "hy_customer_agent-批量删除")
	@ApiOperation(value="hy_customer_agent-批量删除", notes="hy_customer_agent-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@RequiresPermissions("agent:list")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyCustomerAgentService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_customer_agent-通过id查询", notes="hy_customer_agent-通过id查询")
	@GetMapping(value = "/queryById")
	@RequiresPermissions("agent:list")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyCustomerAgent hyCustomerAgent = hyCustomerAgentService.getById(id);
		if(hyCustomerAgent==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyCustomerAgent);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyCustomerAgent
    */
    @RequestMapping(value = "/exportXls")
	@RequiresPermissions("agent:list")
    public ModelAndView exportXls(HttpServletRequest request, HyCustomerAgent hyCustomerAgent) {
        return super.exportXls(request, hyCustomerAgent, HyCustomerAgent.class, "hy_customer_agent");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@RequiresPermissions("agent:list")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyCustomerAgent.class);
    }

}
